<template>
  <div>
    <ul class="myUl">
      <li v-for="(x,index) in dataList">
        <el-row class="mymarginTop_10px">
          <el-upload
            class="avatar-uploader"
            :disabled="is_modifly"
            :action="uploadUrl"
            :show-file-list="false"
            :before-upload="beforeAvatarUpload"
            :data="postData"
            :on-success="handleAvatarSuccess">
            <img v-if="x.thumbnailUrl" :src="x.thumbnailUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-row>
        <el-button type="primary" icon="el-icon-close" class="myclose" @click="onClose(index)" title="移除"></el-button>
      </li>
      <el-button type="primary" icon="el-icon-plus" class="myadd" @click="onAdd" title="添加">添加一项</el-button>
    </ul>
  </div>
</template>

<style scoped>
  .myUl > li{
    box-sizing: border-box;
    border:1px solid transparent;
    position: relative;
  }
  .myUl > li::after{
    content: " ";
    display: block;
    height: 1px;
    background: #DCDFE6;
    margin-top:10px; 
  }
  .myUl > li:hover{
    border:1px dashed #F56C6C;
  }
  .myUl > li:hover .myclose{
    display: block;
  }
  .mymarginTop_10px{
    margin-top: 10px;
  }
  .myclose{
    position: absolute;
    top: 0;
    right: 0;
    width:40px;
    height: 40px;
    display: none;
    background: #F56C6C;
    padding:0;
    border:none;
    color: #fff;
  }
  .myadd{
    width:100%;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<style>
  .myclose i,.myadd i{
    color: #fff;
  }
</style>
<script>
  import { getQNToken } from '@/api/contentMGT';
  import { QINIU_API , QINIU_UP_API } from '@/config/baseUrl';
  export default{
    data(){
      return {
        // imageUrl: '',
        thumbnailUrl:"",
        is_modifly:false,
        postData:{
          token:""
        },
        uploadUrl:QINIU_UP_API,
      }   
    },
    props: ['DataList'],
    components: {
    },
    watch: {
      dataList(val){
        this.$emit("getChildData",this.dataList);//发射数据
      }
    },
    created(){
      let vm=this;
      vm.dataList=vm.DataList;
      vm.onGetToken();
    },
    methods:{
      onGetToken(){//拉取七牛token
        let vm=this;
        getQNToken().then(response => {
          let res=response.data.up_token;
          vm.postData.token=res;
        }).catch(error => {
        })
      },
      getComplaint_chargeuser(val,index){
        this.dataList[index].range=val;
      },
      onClose(index){
        this.dataList.splice(index,1);
      },
      handleChange(val){

      },
      onAdd(){
        this.dataList.push(
          {}
        );
      },
      handleAvatarSuccess(res, file) {
        // this.imageUrl = URL.createObjectURL(file.raw);
        this.thumbnailUrl = QINIU_API + res.key;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isPNG = file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG && !isPNG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }
        return (isJPG || isPNG) && isLt2M;
      }
    }
  }

</script>



